package com.ccgk.logistics.vo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CompanyTypeData {
	
	static String path;
	
	InputStream is = null;
	
	static List<CompanyTypeVO> list = null;
	
	static Map map = new HashMap();
	
	public CompanyTypeData(String path){
		this.path = path;
		if(list==null){
			parseXml();
		}
		
	}
	
	public CompanyTypeData(InputStream is) {
		this.is = is;
	}

	public static String getValue(String key){
		return (String)map.get(key);
	}
	public static List<CompanyTypeVO> getListData(){
		return list;
	}

	private  List<CompanyTypeVO> parseXml() {
		SAXReader reader = new SAXReader();
		InputStream is;
		Document document = null;
		try {
			is = new FileInputStream(path);
			document = reader.read(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		try {
//			document = reader.read(is);
//		} catch (DocumentException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		Element rootElement = document.getRootElement();
		Iterator iter = rootElement.elementIterator();
		
		list = new ArrayList<CompanyTypeVO>();
		
		while(iter.hasNext()){
				Element e = (Element)iter.next();
				CompanyTypeVO vo = new CompanyTypeVO();
				Attribute attr1 = e.attribute(0);
				vo.setAbbre(attr1.getText());
				Attribute attr2 = e.attribute(1);
				vo.setName(attr2.getText());
				
				map.put(vo.getAbbre(), vo.getName());
				list.add(vo);
		}
		return list;
	}
	
	public class CompanyTypeVO{
		private String abbre;
		
		private String name;

		public String getAbbre() {
			return abbre;
		}

		public void setAbbre(String abbre) {
			this.abbre = abbre;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
	
	
}
